package com.cyy.common.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.common.constant.Constants;
import com.cyy.common.domain.BaseEntity;
import com.cyy.common.exception.ProcessException;
import com.cyy.common.mapper.RoleMapper;
import com.cyy.common.pojo.dto.common.OptionDto;
import com.cyy.common.pojo.dto.RoleFormDto;
import com.cyy.common.pojo.entity.Role;
import com.cyy.common.pojo.vo.system.SystemRoleVo;
import com.cyy.common.service.RoleService;
import com.cyy.common.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 曹云友
 * @since 2024-03-29
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    public IPage<SystemRoleVo> getPage(String keywords) {
        return baseMapper.getPage(PageUtils.getPage(), keywords);
    }

    @Override
    public List<OptionDto<String, Integer>> getRoleOptions(String keywords) {
        List<Role> roles = baseMapper.selectList(Wrappers.<Role>lambdaQuery()
                .select(BaseEntity::getId, Role::getName)
                .like(StringUtils.isNoneBlank(keywords), Role::getName, keywords)
                .or()
                .like(StringUtils.isNoneBlank(keywords), Role::getCode, keywords)
        );

        if (CollectionUtil.isEmpty(roles)) {
            return null;
        }

        return roles.stream().map(item -> {
            OptionDto<String, Integer> optionDto = new OptionDto<>();
            optionDto.setLabel(item.getName());
            optionDto.setValue(item.getId());
            return optionDto;
        }).collect(Collectors.toList());

    }

    @Override
    public RoleFormDto getRoleForm(Integer id) {
        Role role = Optional.ofNullable(baseMapper.selectById(id))
                .orElseThrow(() -> new ProcessException("未找到对应角色信息"));

        RoleFormDto formDto = new RoleFormDto();

        BeanUtils.copyProperties(role, formDto);

        return formDto;
    }

    @Override
    public void addRole(RoleFormDto req) {

        // 参数判断
        Assert.isFalse(StringUtils.isAnyBlank(req.getCode(), req.getName())
                , "参数错误！");

        // 判断角色CODE 是否存在
        Assert.isFalse(baseMapper.exists(Wrappers.<Role>lambdaQuery()
                        .eq(Role::getCode, req.getCode()))
                , "已存在对应CODE的角色信息，请勿重复创建");

        Role role = new Role();
        BeanUtils.copyProperties(req, role);
        role.setId(null);

        this.save(role);
    }

    @Override
    public void updateRole(RoleFormDto req) {
        // 参数判断
        Assert.isFalse(
                Objects.isNull(req.getId()) ||
                        StringUtils.isAnyBlank(req.getCode(), req.getName())
                , "参数错误！");
        // 判断 角色CODE 是否存在
        Assert.isFalse(baseMapper.exists(Wrappers.<Role>lambdaQuery()
                        .ne(BaseEntity::getId, req.getId())
                        .eq(Role::getCode, req.getCode()))
                , "已存在对应CODE的角色信息，请勿重复创建");
        Role role = new Role();
        BeanUtils.copyProperties(req, role);

        this.updateById(role);
    }

    @Override
    public void deleteRoles(String ids) {
        List<String> list = Arrays.asList(ids.split(Constants.COMMA));
        if (CollectionUtil.isEmpty(list)) {
            return;
        }

        baseMapper.update(Wrappers.<Role>lambdaUpdate()
                .set(BaseEntity::getStatus, Boolean.FALSE)
                .in(BaseEntity::getId, list)
        );
    }
}
